package Chapter;





/**
 * test
 *
 * @author 翁老师
 * @since 2025/6/3 21:07
 */

class Point {
    private int x;
    private int y;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public void setX(int x) {
        this.x = x;
    }
    // 重定义toString()方法，以 (x,y)显示一个点的信息
    @Override
    public String toString() {
        return "(" + x + "," + y + ")";
    }

    // 添加克隆方法
    public Point clone() {
        return new Point(x, y);
    }
}

class Line {
    private Point p1;
    private Point p2;
    public Line(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    // 添加深拷贝方法
    public Line deepClone() {
        return new Line(p1.clone(), p2.clone());
    }

    @Override
    public String toString() {
        return "[" + p1 + "," + p2 + "]";
    }
}

public class DeepCloneTest {
    public static void main(String[] args) throws Exception {
        Point p1 = new Point(1,1);
        Point p2 = new Point(2,2);

        Line line = new Line(p1, p2);
        // 使用深拷贝创建新对象
        Line lineClone = line.deepClone();

        System.out.println("line修改前：");
        System.out.println("line:" + line);
        System.out.println("lineClone:" + lineClone);

        p1.setX(-1);
        System.out.println("line修改后：");
        System.out.println("line:" + line);
        System.out.println("lineClone:" + lineClone);
    }
}